# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyKDE4.kio import *
from PyKDE4.kdeui import *
from PyKDE4.kdecore import *
from PyKDE4.plasma import Plasma
from PyKDE4 import plasmascript
from settings import SettingsDialog
import twitter

class twi(plasmascript.Applet):
    def __init__(self,parent,args=None):
        plasmascript.Applet.__init__(self,parent)
 
    def init(self):
        self.uname=''
        self.upass=''
        #We have
        self.setHasConfigurationInterface(True)
        self.setAspectRatioMode(Plasma.IgnoreAspectRatio)
        self.theme = Plasma.Svg(self)
        self.theme.setImagePath("widgets/background")
        self.setBackgroundHints(Plasma.Applet.DefaultBackground)
        #The items are vertically sorted
        self.layout = QGraphicsLinearLayout(Qt.Vertical,self.applet)
        #Settings Dialog in settings.py
        #We have 3 labels for the latest messages
        self.msg1 = Plasma.Label(self.applet)
        self.msg2= Plasma.Label(self.applet)
        self.msg3= Plasma.Label(self.applet)
        #LineEdit for posting new message
        self.newmsg=Plasma.LineEdit(self.applet)
        #Button to send a message
        self.SendButton=Plasma.PushButton(self.applet)
        self.SendButton.setText("Update Status")
        #Adding all to the layout
        self.layout.addItem(self.msg3)
        self.layout.addItem(self.msg2)
        self.layout.addItem(self.msg1)
        self.layout.addItem(self.newmsg)
        self.layout.addItem(self.SendButton)
        #Setting layout as our current layout
        self.applet.setLayout(self.layout)
        #Creating timer to update tweets
        self.timer=QTimer()
        self.resize(300,500)
        #connecting timer to the update function
        self.connect(self.timer, SIGNAL("timeout()"),self.update)
        #connecting button to send funtion
        self.connect(self.SendButton,SIGNAL("clicked()"),self.sendmsg)
        #At the start we are showing our current conf
        self.showConfigurationInterface()
        
    def update(self):
        #It does almost all the work
        
        timeline=self.api.GetFriendsTimeline()
        count=1;
        for ustatus in timeline:
            if count==1:
                self.msg3.setText(ustatus.user.name+': '+ustatus.text)
                count=count+1
            elif count==2:
                self.msg2.setText(ustatus.user.name+': '+ustatus.text)
                count=count+1
            elif count==3:
                self.msg1.setText(ustatus.user.name+': '+ustatus.text)
                count=count+1
            else:
                pass
        #Update interval is 150 s
        
        self.timer.start(150)

    def sendmsg(self):
    #This function sends a message when SendButton is clicked
        if self.newmsg.text()=='':
            pass
        #checking if it is 140 characters or lesser
        elif len(self.newmsg.text())<=140:
            mess=self.newmsg.text()
            status=self.api.PostUpdate(mess)
            self.newmsg.setText('')
            self.update()
           
           
    def showConfigurationInterface(self):
    #Function shows configuration interface
        self.settings_dialog=SettingsDialog(self)
        dialog=KPageDialog()
        dialog.setFaceType(KPageDialog.Plain)
        dialog.setButtons(KDialog.ButtonCode(KDialog.Ok | KDialog.Cancel))
        pag=dialog.addPage(self.settings_dialog,u'Settings')
        #When we accept the config it executes configAccepted() funtion
        self.connect(dialog,SIGNAL("okClicked()"),self.configAccepted)
        self.connect(dialog,SIGNAL("cancelClicked()"),self.configDiscarded)
        dialog.resize(350,200)
        dialog.exec_()
     
    def configAccepted(self):
    #it gets current settings and writes them to the config file and the creates twitter api, and starts to get tweets
        self.settings=self.settings_dialog.get_settings()
        self.uname=str(self.settings["user"])
        self.upass=str(self.settings["password"])
        self.settings_dialog.writeToFile()
        self.api=twitter.Api(username=self.uname,password=self.upass)
        self.update()

    def configDiscarded(self):
    #Doing nothing and just closing config window
        pass

def CreateApplet(parent):
    return twi(parent) 
